layui.config({
    base: '../../../eui/plugins/layui-extend/' //这是你存放拓展模块的根目录
});

layui.use(['form'], function () {
    var form = layui.form;
    var id = getParam("id");
    dict2Select("dataWindowType","dataWindowType"); 
    dict2Select("timeUnit","timeWindowUnit"); 
    dict2Select("priorityStrategy","priorityStrategy");
    dict2Select("statFunc","statFunc");
    dict2Select("outputType","outputType"); 
    dict2Select("statDataSource","type"); 
    form.render();
	io.send("/mm/datastreams?pageNo=1&pageSize=-1","GET",null,function(res){
		var data=res.data.list;
		var map=new Map();
		for(var i in data){
			var d=data[i];
    		map.set(d.id.toString(),d.datastreamName);
		}
		map2Select(map,'dsId');
	    form.render();
	},false,false,false,false);
	io.send("/mm/datastreams?pageNo=1&pageSize=-1","GET",null,function(res){
		var data=res.data.list;
		var words=new Map();
		for(var i in data){
			var d=data[i];
    		words.set(d.keyColumn,d.keyColumn);
		}
		map2Select(words,'keyword');
	    form.render();
	},false,false,false,false);
    if(!isNull(id)){
		$("#name").attr("disabled","disabled");
    	io.get('/mm/variables/' + id,function(res){
    		var data = res.data;
		    if(!isNull(data.dsId)){
				io.send("/mm/datastreams/"+data.dsId+"/fields","GET",null,function(res){
					var data=res.data;
					var map=new Map();
					for(var i in data){
						var d=data[i];
			    		map.set(d.innerCName,d.innerCName);
					}
					map2Select(map,'statField');
				},false,false,false,false);
			}
            var form = layui.form;
            //表单初始赋值
            form.val('lForm', data)
            form.render();
			initDataWindowArea();
    	});
    }else{
		    form.val('lForm', {"dataWindowType":"1","type":"1","priority":5});
            form.render();
			initDataWindowArea();
	}
	form.on('select(dsId)', function(data){
		if(!isNull(data.value)){
			io.send("/mm/datastreams/"+data.value+"/fields","GET",null,function(res){
				var data=res.data;
				var map=new Map();
				for(var i in data){
					var d=data[i];
		    		map.set(d.innerCName,d.innerCName);
				}
				map2Select(map,'statField');
			    form.render();
			},false,false,false,false);
		}
	});      
	form.on('select(dataWindowType)', function(data){
		initDataWindowArea();
	});    
    //监听提交
    form.on('submit(save)', function (form) {
        var data=form.field;
		var type=data.dataWindowType;
		if(type==='1'){
			data.priorityStrategy='';
		}else{
			data.timeWindowUnit='';
		}
		if(isNull(data.dsId)&&isNull(data.statField)&&isNull(data.expression)){
			layer.msg("数据流、表达式不能都为空", {icon: 2});
			return false;
		}
		io.post('/mm/variables',JSON.stringify(data),function(res){
	               setTimeout(function(){return closePage();},1000);
	               window.parent.refreshTableData();
		});
		return false;
    });
});


function initDataWindowArea(){
	var dataWindowType = $("#dataWindowType").val();
	if(dataWindowType==='1'){
		$("#tarea").show();
		$("#parea").hide();
	}else if(dataWindowType==='2'){
		$("#tarea").hide();
		$("#parea").show();
	}
}